home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SAMPLE / SERVICE / SERVICE.CPP next >
C/C++ Source or Header  |  1995-07-19  |  4KB  |  158 lines

  1. #include "wfc.h"
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like.
  10. */
  11.  
  12. void list_services( LPCTSTR );
  13. void print_error( LPCTSTR, DWORD );
  14. void usage( void );
  15.  
  16. int main( int argc, char *argv[] )
  17. {
  18.    if ( argc < 2 )
  19.    {
  20.       list_services( NULL );
  21.    }
  22.    else
  23.    {
  24.       if ( strcmpi( argv[ 1 ], "/?" ) == 0 )
  25.       {
  26.          usage();
  27.       }
  28.       else
  29.       {
  30.          list_services( argv[ 1 ] );
  31.       }
  32.    }
  33.  
  34.    return( EXIT_SUCCESS );
  35. }
  36.  
  37. void list_services( LPCTSTR machine_name )
  38. {
  39.    CServiceControlManager manager;
  40.  
  41.    if ( manager.Open( SC_MANAGER_ENUMERATE_SERVICE, NULL, machine_name ) != TRUE )
  42.    {
  43.       print_error( "Can't open service control manager", manager.GetErrorCode() );
  44.    }
  45.  
  46.    if ( machine_name == NULL )
  47.    {
  48.       printf( "Services Running:\n" );
  49.    }
  50.    else
  51.    {
  52.       printf( "Services Running on %s:\n", machine_name );
  53.    }
  54.  
  55.    if  ( manager.EnumerateStatus() == TRUE )
  56.    {
  57.       CStringArray services;
  58.       CStringArray display_names;
  59.       CServiceNameAndStatus status;
  60.  
  61.       int longest_service = 0;
  62.  
  63.       while( manager.GetNext( status ) == TRUE )
  64.       {
  65.          if ( status.ServiceStatus.dwCurrentState == SERVICE_RUNNING )
  66.          {
  67.             services.Add( status.lpServiceName );
  68.             display_names.Add( status.lpDisplayName );
  69.  
  70.             if ( strlen( status.lpServiceName ) > longest_service )
  71.             {
  72.                longest_service = strlen( status.lpServiceName );
  73.             }
  74.          }
  75.       }
  76.  
  77.       /*
  78.       ** Now sort them for easy reading
  79.       */
  80.  
  81.       BOOL sorted = FALSE;
  82.       int here    = 0;
  83.       int last_element = services.GetUpperBound();
  84.  
  85.       while( sorted == FALSE )
  86.       {
  87.          sorted = TRUE;
  88.          here   = 0;
  89.  
  90.          while( here < last_element )
  91.          {
  92.             if ( services[ here ].CompareNoCase( services[ here + 1 ] ) > 0 )
  93.             {
  94.                CString temp_string;
  95.  
  96.                temp_string = services[ here ];
  97.                services[ here ] = (LPCTSTR) services[ here + 1 ];
  98.                services[ here + 1 ] = (LPCTSTR) temp_string;
  99.  
  100.                temp_string = display_names[ here ];
  101.                display_names[ here ] = (LPCTSTR) display_names[ here + 1 ];
  102.                display_names[ here + 1 ] = (LPCTSTR) temp_string;
  103.  
  104.                sorted = FALSE;
  105.             }
  106.  
  107.             here++;
  108.          }
  109.       }
  110.  
  111.       here = 0;
  112.  
  113.       while( here <= last_element )
  114.       {
  115.          printf( "%-*s \"%s\"\n", longest_service, (LPCTSTR) services[ here ], (LPCTSTR) display_names[ here ] );
  116.          here++;
  117.       }
  118.    }
  119.    else
  120.    {
  121.       print_error( "Can't enumerate status\n", manager.GetErrorCode() );
  122.    }
  123. }
  124.  
  125. void usage( void )
  126. {
  127.    printf( "Show active services on a machine. Samuel R. Blackburn\n\n" );
  128.    printf( "Service [[/?][machine_name]]\n\n" );
  129.    printf( "Examples:\n" );
  130.    printf( "\"service /?\"         displays this screen\n" );
  131.    printf( "\"service\"            shows active services on this your machine\n" );
  132.    printf( "\"service Enterprise\" shows active services on the machine named Enterprise\n" ); 
  133. }
  134.  
  135. void print_error( LPCTSTR message, DWORD error_code )
  136. {
  137.    LPVOID message_buffer = (LPVOID) NULL;
  138.  
  139.    FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  140.                   NULL,
  141.                   error_code,
  142.                   MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
  143.         (LPTSTR) &message_buffer,
  144.                   0,
  145.                   NULL );
  146.  
  147.    if ( message_buffer != NULL )
  148.    {
  149.       printf( "%s\nError is %s\n", message, (LPCTSTR) message_buffer );
  150.  
  151.       LocalFree( message_buffer );
  152.    }
  153.    else
  154.    {
  155.       printf( "%s\nError Code is %d\n", message, error_code );
  156.    }
  157. }
  158.